﻿using System.Collections.Generic;
using System.Windows.Forms;
using md.imi.membranes.objects.membranes;
using DigitalRune.Windows.Docking;
using md.imi.membranes.objects.rules;
using md.imi.membranes.objects.strings;
using System;

namespace md.imi.membranes.gui.Editor
{
    public partial class MembraneProperties : DockableForm
    {
        public MembranesTree Membranes { get; set; }

        /// <summary>
        ///     Default constructor
        /// </summary>
        public MembraneProperties()
        {
            InitializeComponent();
        }

        /// <summary>
        ///     Load data into form controls
        /// </summary>
        /// <param name="membrane"></param>
        public void LoadData(MembranesTree membrane)
        {
            lbMembranes.Items.Clear();
            lbRules.Items.Clear();
            lbMultisets.Items.Clear();
            Membranes = membrane;
            if (membrane == null)
            {
                return;
            }

            Text = "Membrane {" + membrane.Id + "}";
            tbMembraneLabel.Text = membrane.Label;

            ReloadMultisets();
            ReloadRules();
            ReloadNestedMembranes();

        }

        private void ReloadMultisets()
        {
            lbMultisets.Items.Clear();
            if (Membranes.Multiset != null)
            {
                foreach (var item in Membranes.Multiset)
                {
                    lbMultisets.Items.Add(item.ToString());
                }
            }
            
        }

        private void ReloadRules()
        {
            lbRules.Items.Clear();
            if (Membranes.Rules != null)
            {
                foreach (var item in Membranes.Rules)
                {
                    lbRules.Items.Add(item);
                }
            }
            
        }

        private void ReloadNestedMembranes()
        {
            lbMembranes.Items.Clear();
            if (Membranes.Membranes != null)
            {
                foreach (var item in Membranes.Membranes)
                {
                    lbMembranes.Items.Add(item);
                }
            }
  
        }



        /// <summary>
        ///     Collect data from form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, System.EventArgs e)
        {
            Membranes.Label = tbMembraneLabel.Text;

            // get other values
        }

        /// <summary>
        ///     Handle click on multisets tool bar buttons click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripMultisetsButtonClick(object sender, System.EventArgs e)
        {
            if (sender == tsEditMultiset && lbMultisets.SelectedItem != null)
            {
                var me = new MultisetEditor(Membranes.Label);
                me.LoadData(Membranes.Multiset[lbMultisets.SelectedIndex]);
                if (me.ShowDialog() == DialogResult.OK)
                {
                    Membranes.Multiset[lbMultisets.SelectedIndex] = me.GetData();
                    ReloadMultisets();
                }
            }

            if (sender == tsAddMultiset)
            {
                var me = new MultisetEditor(Membranes.Label);
                me.LoadData(new StringObject());
                if (me.ShowDialog() == DialogResult.OK)
                {
                   Membranes.Multiset.Add(me.GetData());
                   ReloadMultisets();
                }
            }

            if (sender == tsRemoveMultiset && lbMultisets.SelectedIndex != -1)
            {
                Membranes.Multiset.RemoveAt(lbMultisets.SelectedIndex);
                ReloadMultisets();
            }
        }

        private void ToolStripRulesButtonClick(object sender, System.EventArgs e)
        {
            if (sender == tsEditRule && lbRules.SelectedItem != null)
            {
                var re = new RulesEditor(Membranes.Label);
                re.LoadData(Membranes.Rules[lbRules.SelectedIndex]);
                if (re.ShowDialog() == DialogResult.OK)
                {
                    Membranes.Rules[lbRules.SelectedIndex] = re.GetData();
                    ReloadRules();
                }

            }

            if (sender == tsAddRule)
            {
                var re = new RulesEditor(Membranes.Label);
                re.LoadData(new ReplicationRule(Membranes));
                if (re.ShowDialog() == DialogResult.OK)
                {
                    Membranes.Rules.Add(re.GetData());
                    ReloadRules();
                }


            }

            if (sender == tsRemoveRule && lbRules.SelectedIndex != -1)
            {
                Membranes.Rules.RemoveAt(lbRules.SelectedIndex);
                ReloadRules();
            }

        }

        private void ToolStripMembranesButtonClick(object sender, System.EventArgs e)
        {
            if (sender == tsEditMembrane && lbMembranes.SelectedItem != null)
            {
                var mEdit = new MembraneEdit(Membranes.Label, Membranes.Membranes[lbMembranes.SelectedIndex]);

                if (mEdit.ShowDialog() == DialogResult.OK)
                {
                    Membranes.Membranes[lbMembranes.SelectedIndex] = mEdit.GetData();
                    ReloadNestedMembranes();
                }
            }

            if (sender == tsAddMembrane)
            {
                var mEdit = new MembraneEdit(Membranes.Label, new MembranesTree());

                if (mEdit.ShowDialog() == DialogResult.OK)
                {
                    Membranes.Membranes.Add(mEdit.GetData());
                    ReloadNestedMembranes();
                }
            }

            if (sender == tsRemoveMembrane && lbMembranes.SelectedItem != null)
            {
                Membranes.Membranes.RemoveAt(lbMembranes.SelectedIndex);
                ReloadNestedMembranes();
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbMultisets_DoubleClick(object sender, System.EventArgs e)
        {
            ToolStripMultisetsButtonClick(tsEditMultiset, null);
        }

        private void lbRules_DoubleClick(object sender, System.EventArgs e)
        {
            ToolStripRulesButtonClick(tsEditRule, null);
        }

        private void lbMembranes_DoubleClick(object sender, System.EventArgs e)
        {
            ToolStripMembranesButtonClick(tsEditMembrane, null);
        }
    }
}